package com.lewis.xuanke.controller;

import com.lewis.xuanke.entity.ComResult;
import com.lewis.xuanke.entity.User;
import com.lewis.xuanke.mapper.UserMapper;

import com.lewis.xuanke.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    //登录接口
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ComResult<User> login(String account, String pwd, Integer type) {
        User User = new User();
        User.setAccount(account);
        User.setPwd(pwd);
        User.setType(type);

        User login = userMapper.login(User);
        ComResult<User> result = new ComResult<>();
        if (login == null) {
            result.setCode(1);
            result.setMsg("密码不正确");
        } else {
            result.setCode(0);
            result.setMsg("登录成功");
        }

        result.setData(login);
        return result;
    }

    //用户信息
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public ComResult<User> getUserInfo(Integer id) {
        User login = userMapper.selectByPrimaryKey(id);
        ComResult<User> result = new ComResult<>();
        if (login == null) {
            result.setMsg("fail");
        } else {
            result.setMsg("success");
        }
        result.setCode(0);
        result.setData(login);
        return result;
    }

    //用户信息
    @RequestMapping(value = "/getAllUser", method = RequestMethod.POST)
    public ComResult<List<User>> getAllUser(Integer type) {
        List<User> login = userMapper.selectAll();
        List<User> users = new ArrayList<>();
        ComResult<List<User>> result = new ComResult<>();
        for (int i = 0; i < login.size(); i++) {
            User user = login.get(i);
            if (user.getType() == type) {
                users.add(user);
            }
        }
        if (login.size() == 0) {
            result.setMsg("fail");
        } else {
            result.setMsg("success");
        }
        result.setCode(0);
        result.setData(users);
        return result;
    }

    //搜索用户
    @RequestMapping(value = "/searchUser", method = RequestMethod.POST)
    public ComResult<List<User>> searchUser(Integer type, String id) {
        List<User> login = userMapper.selectAll();
        List<User> users = new ArrayList<>();
        ComResult<List<User>> result = new ComResult<>();
        for (int i = 0; i < login.size(); i++) {
            User user = login.get(i);
            if (type.equals(user.getType()) &&
                    (user.getId().toString().contains(id) || user.getName().contains(id))) {
                users.add(user);
            }
        }
        if (login.size() == 0) {
            result.setMsg("fail");
        } else {
            result.setMsg("success");
        }
        result.setCode(0);
        result.setData(users);
        return result;
    }

    //注册
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ComResult register(String pwd, String account, String name, String xueyuan, String banji, String zhuanye, Integer type, String img) throws Exception {
        User User = new User();
        User.setType(type);
        User.setPwd(pwd);
        User.setName(name);
        User.setXueyuan(xueyuan);
        User.setBanji(banji);
        User.setZhuanye(zhuanye);
        User.setImg(img);
        User.setAccount(account);
        ComResult result = new ComResult<>();
        User check = userMapper.check(User);
        if (check == null) {
            int i = userMapper.insert(User);
            if (i == 1) {
                result.setMsg("注册成功");
            } else {
                result.setMsg("注册失败");
            }
        } else {
            result.setMsg("学号已存在");
        }
        result.setCode(0);
        return result;
    }

    //注册
    @RequestMapping(value = "/upUser", method = RequestMethod.POST)
    public ComResult upUser(String name, String xueyuan, String banji, String zhuanye, Integer type, String img, Integer id) throws Exception {
        User User = new User();
        User.setType(type);
        User.setName(name);
        User.setXueyuan(xueyuan);
        User.setBanji(banji);
        User.setZhuanye(zhuanye);
        User.setImg(img);
        User.setId(id);
        ComResult result = new ComResult<>();
        int i = userMapper.insert(User);
        if (i == 1) {
            result.setMsg("更新成功");
        } else {
            result.setMsg("失败");
        }
        result.setCode(0);
        return result;
    }

    //更新
    @RequestMapping(value = "/upPwd", method = RequestMethod.POST)
    public ComResult upPwd(Integer id, String pwd) {
        User user = new User();
        ;
        user.setPwd(pwd);
        user.setId(id);

        int i = userMapper.upPwd(user);
        ComResult result = new ComResult<>();
        if (i == 1) {
            result.setMsg("更新成功");
        } else {
            result.setMsg("更新失败");
        }
        result.setCode(0);

        return result;
    }

    //更新
    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    public ComResult delUser(Integer id) {
        int i = userMapper.deleteByPrimaryKey(id);
        ComResult result = new ComResult<>();
        if (i == 1) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
        }
        result.setCode(0);
        return result;
    }
}
